package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.entity.Teacher;
import com.example.yuntiku.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TeacherDao extends BaseDao {
    //    SELECT `uid`,`uname`,`upwd`,`email`,`cid`,`houseid` FROM `users`
    public Teacher selectUsers(String email, String tpwd) {
        Connection conn=null;
        PreparedStatement stmt=null;
        ResultSet rs=null;
        try {
            conn=super.getConnection();
            stmt=conn.prepareStatement("SELECT `tid`,`tname`,`tpwd`,`email` FROM `teacher` WHERE `email`=? AND `tpwd`=?");
            stmt.setString(1,email);
            stmt.setString(2,tpwd);
            rs=stmt.executeQuery();
            while (rs.next()) {
                Teacher entity = new Teacher();
                entity.setTid(rs.getInt("tid"));
                entity.setTname(rs.getString("tname"));
                entity.setTpwd(rs.getString("tpwd"));
                entity.setEmail(rs.getString("email"));
                return entity;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn,stmt,rs);
        }
        return null;
    }

    public int insert(Teacher teacher){
        String sql="INSERT INTO `yuntiku`.`teacher` (`email`,`tname`,`tpwd`,`permit`) VALUES(?,?,?,?) ;";
        return super.executeUpdate(sql,
                teacher.getEmail(),
                teacher.getTname(),
                teacher.getTname(),
                teacher.getPermit());
    }
    public int update(Teacher teacher){
        String sql="UPDATE `yuntiku`.`teacher` SET `tpwd` = ? WHERE `email` = ?;";
        return super.executeUpdate(sql,
                teacher.getTpwd(),
                teacher.getEmail());
    }
    public boolean isEmailRegistered(String email) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT COUNT(*) FROM `teacher` WHERE `email` = ?");
            stmt.setString(1, email);
            rs = stmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, stmt, rs);
        }
        return false;
    }

    public Teacher selectById(int id){
        Teacher users =null;
        Connection conn=null;
        PreparedStatement stmt=null;
        ResultSet rs=null;
        try {
            conn=super.getConnection();
            stmt=conn.prepareStatement("SELECT `tid`,`tname`,`tpwd`,`email` FROM `teacher` WHERE `tid`=?");
            stmt.setInt(1,id);
            rs=stmt.executeQuery();
            while (rs.next()) {
                Teacher entity = new Teacher();
                entity.setTid(rs.getInt("tid"));
                entity.setTname(rs.getString("tname"));
                entity.setTpwd(rs.getString("tpwd"));
                entity.setEmail(rs.getString("email"));
                return entity;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return users;
    }

}
